FriendLevelingMonitor_View = CreateFrame("Frame","FriendLevelingMonitor_View");

local SHOWELEMENTS = 10
local TimeSinceLastUpdate = 0
local FLM_UpdateInterval = 1
local SortedList = {}
local Bars = {}

-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
-- Helper functions
local function tContains(table, item)
       local index = 1;
       while table[index] do
               if ( item.name == table[index].name ) then
                       return index;
               end
               index = index + 1;
       end
       return nil;
end

local function tUpdate(table, idx, item)
       table[idx].xp,table[idx].lvl,table[idx].status,table[idx].lastchanged = item.xp, item.lvl, item.status, item.lastchanged
end

local function compare(a,b)
	if a.lvl == b.lvl then
		if a.xp > b.xp then
			return true
		end
	elseif a.lvl > b.lvl then
		return true
	end
	return false
end
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
local BackdropSB = {
	bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
	edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
	tile = true,
	tileSize = 16,
	edgeSize = 12,
	insets = {
		left = 3,
		right = 3,
		top = 3,
		bottom = 3,
	},
}

local BackdropFull = {
	bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
	edgeFile = nil,
	tile = true,
	tileSize = 16,
	edgeSize = 0,
	insets = {
		left = 0,
		right = 0,
		top = 0,
		bottom = 0,
	},
}

local FLM_BG = CreateFrame("Frame", nil, UIParent)
FLM_BG:SetPoint("CENTER", UIParent, "CENTER", 0,0)
FLM_BG:SetBackdrop(BackdropFull)
FLM_BG:SetWidth(GetScreenWidth())
FLM_BG:SetHeight(GetScreenHeight())
FLM_BG:SetBackdropColor(0, 0, 0, 1)
FLM_BG:Show()

local function CreateStatusBar(character, i)
	barHeight = floor(FLM_BG:GetHeight()/SHOWELEMENTS)
	
	local Bdf = CreateFrame("Frame", nil, FLM_BG)
	Bdf:SetPoint("TOPLEFT", FLM_BG, "TOPLEFT", 0, -((i-1)*barHeight))
	Bdf:SetBackdrop(BackdropSB)
	Bdf:SetWidth(FLM_BG:GetWidth())
	Bdf:SetHeight(barHeight)
	Bdf:SetBackdropColor(0, 0, 0, 1)
	
	local SB = CreateFrame("StatusBar", nil, Bdf)
	SB:SetPoint("TOPLEFT", 3, -3)
	SB:SetPoint("BOTTOMRIGHT", -4, 3)
	SB:SetMinMaxValues(0, 100)
	SB:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
	SB:GetStatusBarTexture():SetHorizTile(false)
	SB:GetStatusBarTexture():SetVertTile(false)
	if (character.status == "OFFLINE") then
		SB:SetStatusBarColor(0.3, 0.3, 0.3)
	elseif (character.status == "INACTIVE") then
		SB:SetStatusBarColor(0.6, 0.6, 0.6)
	else
		SB:SetStatusBarColor(0, 0.65, 0)
	end
	SB:SetValue(character.xp)
	SB.Bdf = Bdf

	SB.tname = SB:CreateFontString(nil, "OVERLAY")
	SB.tname:SetPoint("LEFT", SB, "LEFT", 20, 0)
	SB.tname:SetFont("Fonts\\FRIZQT__.TTF", 16, "OUTLINE")
	SB.tname:SetJustifyH("LEFT")
	SB.tname:SetShadowOffset(1, -1)
	SB.tname:SetTextColor(1, 1, 1)
	SB.tname:SetText(character.name)
	
	SB.tlvl = SB:CreateFontString(nil, "OVERLAY")
	SB.tlvl:SetPoint("RIGHT", SB, "RIGHT", -20, 0)
	SB.tlvl:SetFont("Fonts\\FRIZQT__.TTF", 16, "OUTLINE")
	SB.tlvl:SetJustifyH("LEFT")
	SB.tlvl:SetShadowOffset(1, -1)
	SB.tlvl:SetTextColor(1, 1, 1)
	SB.tlvl:SetText("LVL: "..character.lvl)
	
	SB.txp = SB:CreateFontString(nil, "OVERLAY")
	SB.txp:SetPoint("CENTER", SB, "CENTER", 0, 8)
	SB.txp:SetFont("Fonts\\FRIZQT__.TTF", 16, "OUTLINE")
	SB.txp:SetJustifyH("LEFT")
	SB.txp:SetShadowOffset(1, -1)
	SB.txp:SetTextColor(1, 1, 1)
	SB.txp:SetText(character.xp.."%")	

	SB.tstatus = SB:CreateFontString(nil, "OVERLAY")
	SB.tstatus:SetPoint("CENTER", SB, "CENTER", 0,-8)
	SB.tstatus:SetFont("Fonts\\FRIZQT__.TTF", 16, "OUTLINE")
	SB.tstatus:SetJustifyH("LEFT")
	SB.tstatus:SetShadowOffset(1, -1)
	SB.tstatus:SetTextColor(1, 1, 1)
	SB.tstatus:SetText(character.status)	
	
	SB.name = character.name
	return SB
end

local function UpdateStatusBar(bar, character)
	bar:SetValue(character.xp)
	bar.tname:SetText(character.name)
	bar.txp:SetText(character.xp.."%")	
	bar.tlvl:SetText("LVL: "..character.lvl)
	bar.tstatus:SetText(character.status)	
	bar.name = character.name
	if (character.status == "OFFLINE") then
		bar:SetStatusBarColor(0.3, 0.3, 0.3)
	elseif (character.status == "INACTIVE") then
		bar:SetStatusBarColor(0.6, 0.6, 0.6)
	else
		bar:SetStatusBarColor(0, 0.65, 0)
	end	
end

function FriendLevelingMonitor_View:LevelUp(name)

end

function FriendLevelingMonitor_View:Update(name)
	item = {name=name, xp=FLM_ActiveList[name].xp, lvl=FLM_ActiveList[name].lvl, status=FLM_ActiveList[name].status, lastchanged=FLM_ActiveList[name].lastchanged}
	idx = tContains(SortedList, item)
	if ( idx == nil ) then
		table.insert(SortedList, item)
	else
		tUpdate(SortedList, idx, item)
	end
	table.sort(SortedList, compare)
end


local function refresh(self, elapsed)
	TimeSinceLastUpdate = TimeSinceLastUpdate + elapsed;
	if (TimeSinceLastUpdate >= FLM_UpdateInterval) then
		for i,v in ipairs(SortedList) do
			if ((time()-v.lastchanged)>= FLM_AFKTIME and v.status ~="OFFLINE" ) then v.status="INACTIVE" end
			if (Bars[i] ~= nil )then
				UpdateStatusBar(Bars[i], v)
			else
				Bars[i] = CreateStatusBar(v, i)
			end
		end
		TimeSinceLastUpdate = 0;
	end
end

FriendLevelingMonitor_View:SetScript("OnUpdate", refresh);